﻿// CrackPassword.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include <iostream>


#include "shellExecuteHelper.h"


#pragma warning(disable : 4996)

BOOL EnableDebugPrivilege()
{
    BOOL bSuccess = FALSE;
    HANDLE hToken = NULL;
    DWORD lastSystemError = ERROR_SUCCESS;

    do
    {
        bSuccess = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken);
        if (!bSuccess) {
            lastSystemError = GetLastError();
            break;
        }

        LUID luid;
        bSuccess = LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid);
        if (!bSuccess) {
            lastSystemError = GetLastError();
            break;
        }

        DWORD length = 0;
        TOKEN_PRIVILEGES tkpOld;
        TOKEN_PRIVILEGES tkp;
        tkp.PrivilegeCount = 1;
        tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
        tkp.Privileges[0].Luid = luid;

        bSuccess = AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), &tkpOld, &length);
        if (!bSuccess) {
            lastSystemError = GetLastError();
            break;
        }

        bSuccess = TRUE;
    } while (false);

    return bSuccess;
}

int main()
{
    int retval = -1;
    BOOL bTrue = FALSE;
    DWORD ret = 0;

    std::string executable;
    std::string directory;

    TOKEN_ELEVATION_TYPE tet = TOKEN_ELEVATION_TYPE::TokenElevationTypeDefault;
    BOOL isAdmin = FALSE;

    ShellExecuteHelper seHelper;

    OSVERSIONINFOEXA osVer;

    do
    {
        osVer.dwOSVersionInfoSize = sizeof(osVer);
        bTrue = GetVersionExA((LPOSVERSIONINFOA)&osVer);

#if 0
        DWORD typeMask = 0;
        typeMask |= VER_BUILDNUMBER;
        typeMask |= (VER_MAJORVERSION | VER_MINORVERSION);
        typeMask |= VER_PLATFORMID;
        typeMask |= (VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR);
        typeMask |= VER_SUITENAME;
        typeMask |= VER_PRODUCT_TYPE;

        ULONGLONG conditionMask = 0;
        
        conditionMask = VerSetConditionMask()
        
        bTrue = VerifyVersionInfoA(&osVer, typeMask, conditionMask);
#endif

        

        bTrue = seHelper.GetModuleExecutable(executable);
        bTrue = seHelper.GetModuleDirectory(directory);

        executable = "C:\\Work\\cmd_log.ttt";

        bTrue = seHelper.GetProcessElevation(&tet, &isAdmin);
        if (!bTrue) {
            ::MessageBoxA(NULL, "检查进程状态失败，无法继续执行...", "失败", MB_OK);
            break;
        }

        if (TOKEN_ELEVATION_TYPE::TokenElevationTypeFull != tet) {
            int response = ::MessageBoxA(NULL, "当前进程是未提升状态，要以提升状态启动新进程吗？\r\n点击【确定】提升进程。", "询问", MB_OKCANCEL);
            if (IDOK != response) {
                break;
            }

            std::string error_message;
            //ret = seHelper.LaunchFromProgramWithParameter(executable.c_str(), NULL, directory.c_str(), "runas", &error_message);
            //ret = seHelper.LaunchFromProgramWithParameter(executable.c_str(), NULL, directory.c_str(), NULL, &error_message);
            ret = seHelper.LaunchFromProgramWithParameter(executable.c_str(), NULL, directory.c_str(), "open", &error_message);
            if (ret) {
                ::MessageBoxA(NULL, error_message.c_str(), "提升失败", MB_OK);
                break;
            }
        }
        else
        {
            ::MessageBoxA(NULL, "当前进程为【已提升】状态", "提示", MB_OK);
        }
        
        //bTrue = EnableDebugPrivilege();

        retval = 0;
    } while (false);

    return retval;
}

